﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using DHQGHN.DAL.BO.Admin;

namespace DHQGHN.MVC.Controllers
{
    public class CommonData
    {
        public static string AppCode = "TKTHDS.PM0";
        public static readonly string AppName = "PM Quản lý dữ liệu dùng chung - PM0";
        static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(CommonData));
        public static string GetListId(int[] ids)
        {
            return GetListId("Danh sách id: ", ";", ids);
        }

        public static string GetListId(string[] ids)
        {
            List<int> data = new List<int>();
            int result = 0;
            for (int i = 0; i < ids.Length; i++)
            {
                if (int.TryParse(ids[i], out result))
                {
                    data.Add(result);
                }
            }

            return GetListId("Danh sách id: ", ";", data.ToArray());
        }

        public static string GetListId(string title, string splitter, int[] ids)
        {
            StringBuilder idList = new StringBuilder();
            int len = ids.Length;
            idList.Append(title);
            for (int i = 0; i < len; i++)
            {
                idList.Append(ids[i]).Append(splitter);
            }
            string result = idList.ToString().TrimEnd(splitter.ToCharArray());

            return result;
        }

        public static IEnumerable<SelectListItem> getListApp(string text, string value)
        {
            try
            {
                List<SelectListItem> lstItems = new List<SelectListItem>();
                lstItems.Add(new SelectListItem
                {
                    Value = value,
                    Text = text
                });

                ApplicationBO appBO = new ApplicationBO();
                var lstType = appBO.GetAll();
                foreach (var type in lstType)
                {
                    lstItems.Add(new SelectListItem
                    {
                        Value = type.AppId.ToString(),
                        Text = type.AppName
                    });
                }

                return new SelectList(lstItems, "Value", "Text");
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public static IEnumerable<SelectListItem> GetAllMenuByParentID(int intAppID, int intParentID)
        {
            try
            {
                List<SelectListItem> lstItems = new List<SelectListItem>();
                lstItems.Add(new SelectListItem
                {
                    Value = "-1",
                    Text = "-- Chọn chức năng cha --"
                });

                MenuBO appBO = new MenuBO();
                var lstMenu = appBO.GetAllMenuByParentID(intAppID, intParentID);
                foreach (var type in lstMenu)
                {
                    lstItems.Add(new SelectListItem
                    {
                        Value = type.MenuId.ToString(),
                        Text = type.MenuName
                    });
                }

                return new SelectList(lstItems, "Value", "Text");
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public static IEnumerable<SelectListItem> getListApp()
        {
            return getListApp("-- Chọn ứng dụng --", "0");
        }

        public const string KH_DHQGHN_CODE = "KH_DHQGHN";
    }

}